package elitech.vietnam.weatherfashion.tabs.styler;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.backgroundtask.PostLikeTask;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.custom.LikeButton;
import elitech.vietnam.weatherfashion.custom.ScaleImageView;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.tabs.BaseContainerFragment;
import elitech.vietnam.weatherfashion.ultilities.MyDialogHandler;

public class StylerDetailGridAdapter extends ArrayAdapter<PostDTO> {

	protected StylerDetailFragment fragment;
	protected MemberDTO member;
	protected PostDTO post;

	public StylerDetailGridAdapter(StylerDetailFragment fragment, MemberDTO member, int textViewResourceId, List<PostDTO> objects) {
		super(fragment.getActivity(), textViewResourceId, objects);

		this.fragment = fragment;
		this.member = member;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;

		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(fragment.getActivity());
			row = inflater.inflate(R.layout.layout_selfgriditem, parent, false);
			holder = new ViewHolder();
			holder.image = (ScaleImageView) row.findViewById(R.id.weekly_gridview_image);
			holder.button = (LikeButton) row.findViewById(R.id.weekly_gridview_buttonlike);
			holder.button.setUp(false);
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();

		holder.button.setData(getItem(position).getLikes(), getItem(position).isLiked());
		// ImageLoader.getInstance().displayImage(AppConfig.SERVER_IMAGE_URL +
		// holder.data.getImage(), holder.imageView);
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_THUMB_URL + getItem(position).getImage(), R.drawable.loading_time);
		holder.image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				((BaseContainerFragment) fragment.getParentFragment()).replaceFragment(StylerPostFragment.init(getItem(position), member), true);
			}
		});

		holder.button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				holder.button.setEnabled(false);
				MemberDTO user = GlobalValue.getInstance(getContext()).readUserInfo();
				if (user == null) {
					Utils.openLogin(fragment.getActivity());
				} else {
					String ms = getContext().getString(R.string.do_you_want_to_like_this_post_);
					if (getItem(position).isLiked())
						ms = getContext().getString(R.string.do_you_want_to_unlike_this_post_);
					new MyDialogHandler().confirm(getContext(), getContext().getString(R.string.like_confirm), ms, getContext().getString(R.string.no), getContext().getString(R.string.yes),
							new Runnable() {
								@Override
								public void run() {
									new PostLikeTask(getContext(), holder.button, getItem(position), !getItem(position).isLiked()).execute();
								}
							}, null);
				}
				holder.button.setEnabled(true);
			}
		});

		return row;
	}

	static class ViewHolder {
		ScaleImageView image;
		LikeButton button;
	}

}
